﻿// <copyright file="Embed.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace MediaRss
{
    #region

    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Serialization;

    #endregion

    [XmlRoot(ElementName = "embed", Namespace = "http://search.yahoo.com/mrss/")]
    public class Embed : MediaRssBase, IXmlSerializable
    {
        #region Constants and Fields

        internal const string ELEMENT_NAME = "embed";

        #endregion

        #region Constructors and Destructors

        public Embed()
        {
            this.ParameterItems = new Collection<Parameter>();
        }

        #endregion

        #region Public Properties

        public int Height { get; set; }

        public Collection<Parameter> ParameterItems { get; set; }

        public Uri Url { get; set; }

        public int Width { get; set; }

        #endregion

        #region Public Methods

        public void ReadXml(XmlReader reader)
        {
            bool isEmptyElement = reader.IsEmptyElement;
            if (reader.HasAttributes)
            {
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    reader.MoveToNextAttribute();
                    if (reader.NamespaceURI == "")
                    {
                        if (reader.LocalName == "url")
                        {
                            this.Url = new Uri(reader.Value);
                        }
                        else if (reader.LocalName == "width")
                        {
                            this.Width = int.Parse(reader.Value);
                        }
                        else if (reader.LocalName == "height")
                        {
                            this.Height = int.Parse(reader.Value);
                        }
                        else
                        {
                            base.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                        }
                    }
                }
            }
            reader.ReadStartElement();
            if (!isEmptyElement)
            {
                while (reader.IsStartElement())
                {
                    string localName = reader.LocalName;
                    if ((localName != null) && (localName == "param"))
                    {
                        var item = new Parameter();
                        item.ReadXml(reader);
                        this.ParameterItems.Add(item);
                    }
                    else
                    {
                        base.ElementExtensions.Add((XElement)XNode.ReadFrom(reader));
                    }
                }
            }
            reader.ReadEndElement();
        }

        public void WriteXml(XmlWriter writer)
        {
            if (this.Url != null)
            {
                writer.WriteAttributeString("url", null, this.Url.ToString());
            }
            writer.WriteAttributeString("width", null, this.Width.ToString());
            writer.WriteAttributeString("height", null, this.Height.ToString());
            if ((this.ParameterItems != null) && (this.ParameterItems.Count > 0))
            {
                foreach (var parameter in this.ParameterItems)
                {
                    parameter.WriteXml(writer);
                }
            }
            foreach (var pair in base.AttributeExtensions)
            {
                writer.WriteAttributeString(pair.Key.Name, pair.Key.Namespace, pair.Value);
            }
            foreach (var element in base.ElementExtensions)
            {
                element.WriteTo(writer);
            }
        }

        #endregion
    }
}